package com.intelligent.mapper;

import com.intelligent.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author lenovo
 * @description 针对表【t_user(系统用户表)】的数据库操作Mapper
 * @createDate 2025-08-26 11:51:45
 * @Entity com.intelligent.entity.User
 */
public interface UserMapper extends BaseMapper<User> {
    @Select("SELECT * FROM t_user WHERE username = #{username}")
    User selectByUsername(String username);

    // 根据用户ID查询权限列表（关联RBAC三张表：用户-角色-权限）
    @Select("SELECT p.permission_code " +
            "FROM t_user_role ur " +
            "LEFT JOIN t_role_permission rp ON ur.role_id = rp.role_id " +
            "LEFT JOIN t_permission p ON rp.permission_id = p.permission_id " +
            "WHERE ur.user_id = #{userId}")
    List<String> selectPermissionsByUserId(Long userId);

    @Select("select * from t_user where phone = #{phone}")
    User selectByPhone(String phone);

    /**
     * 获取当前已存在的最大用户编号数值部分（AA前缀后面的数字）。
     * 例如 AA0007 -> 返回 7；若不存在则返回 null。
     */
    @Select("SELECT MAX(CAST(SUBSTRING(user_code, 3) AS UNSIGNED)) FROM t_user WHERE user_code LIKE 'AA%'")
    Integer selectMaxUserCodeSeq();
}
